﻿using UnityEngine;


public class TweenActionRotate : TweenActionBase
{
    private Vector3 value;
    private Vector3 start;
    private Transform target;
    bool to;

    public TweenActionRotate(Transform target, Vector3 value, float totalTime,bool to)
    {
        this.totalTime = totalTime;
        this.target = target;
        this.to = to;
        this.value = value;
    }
    public override void OnStart()
    {
        start = target.localEulerAngles;
        if(to)
        {
            value -= start;

            value = GetNearAngle(value);

        }
    }
    public override void OnUpdate(float schedule)
    {
        if (target == null)
            return;
        //if (to)
        //{
        //    target.localEulerAngles = start * (1 - schedule) + value * schedule;
        //}
        //else
            target.localEulerAngles = start + value * schedule;


    }

    private Vector3 GetNearAngle(Vector3 ori)
    {
        Vector3 result = Vector3.zero;
        result.x = GetNearAngle(ori.x);
        result.y = GetNearAngle(ori.y);
        result.z = GetNearAngle(ori.z);
        return result;
    }
    //获得-180到180度的值
    private float GetNearAngle(float ori)
    {
        float floor = Mathf.Floor(ori / 360);
        ori -= floor * 360f;
        if (ori > 180f)
            ori -= 360f;
        return ori;
    }
}

